package com.huirui.server.controller.admin;

import com.huirui.server.pojo.dto.ProductDto;
import com.huirui.server.pojo.entity.Product;
import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.*;
import com.huirui.server.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminProductController")
@RequestMapping("/admin/product")
@Slf4j
@Validated
@Tag(name = "商品管理", description = "小程序商品管理")
public class ProductController {

    @Resource
    private ProductService productService;

    @GetMapping
    @Operation(summary = "获取商品列表")
    public Result<PageResult<ProductVo>> getProductList(
            @RequestParam("option") String option,
            @RequestParam("page") Long page,
            @RequestParam("pageSize") Long pageSize
    ) {
        PageResult<ProductVo> res = productService.getProductList(option, page, pageSize);
        return Result.success(res);
    }

    @PostMapping
    @Operation(summary = "保存商品")
    public Result<Void> saveProduct(@RequestBody @Valid ProductDto productDto) {
        productService.saveProduct(productDto);
        return Result.success();
    }

    @DeleteMapping
    @Operation(summary = "删除商品")
    public Result<Void> deleteProductById(@RequestParam("id") Long id) {
        productService.deleteProductById(id);
        return Result.success();
    }

    @PatchMapping("/status/{productId}/{status}")
    @Operation(summary = "上架或下架商品")
    public Result<Void> changeProductStatus(
            @Pattern(regexp = "^[01]$", message = "status 只能为 0（下架）或 1（上架）")
            @PathVariable("status") String status,
            @PathVariable("productId") String productId) {
        productService
                .lambdaUpdate()
                .eq(Product::getId, productId)
                .set(Product::getProductStatus, status)
                .update();
        return Result.success();
    }

    @GetMapping("/echo")
    @Operation(summary = "商品数据的回显")
    public Result<ProductEditVo> productEcho(@RequestParam Long id) {
        ProductEditVo productEditVo = productService.productEcho(id);
        return Result.success(productEditVo);
    }

    @PutMapping
    @Operation(summary = "修改商品数据")
    public Result<Void> updateProduct(@RequestBody @Valid ProductDto productDto) {
        productService.updateProduct(productDto);
        return Result.success();
    }

    @GetMapping("/search")
    @Operation(summary = "搜索商品")
    public Result<PageResult<ProductVo>> searchProduct(
            @RequestParam(value = "productName", required = false) String productName,
            @RequestParam(value = "status") String status,
            @RequestParam(value = "productCode", required = false) String productCode,
            @RequestParam(value = "categoryId", required = false)
            @Parameter(
                    description = "商品类型ID（可选参数）,0-普通商品，1-秒杀商品",  // 描述参数含义
                    example = "2",  // 示例值
                    schema = @Schema(type = "long")  // 指定参数类型（可选，通常会自动推断）
            )
            Long categoryId,
            @RequestParam(value = "productType", required = false) Long productType,
            @RequestParam("page") Long page,
            @RequestParam("pageSize") Long pageSize
    ) {
        PageResult<ProductVo> res = productService.searchProduct(productName, productCode, status, categoryId, productType, page, pageSize);
        return Result.success(res);
    }

    @GetMapping("/simple/search")
    @Operation(summary = "简单搜索商品")
    public Result<List<SimpleProductVo>> searchBriefProductInfo(
            @RequestParam(value = "keyword") String keyword) {
        List<SimpleProductVo> list = productService.searchBriefProductInfo(keyword);
        return Result.success(list);
    }


    @GetMapping("/coupon/product/info")
    @Operation(summary = "优惠券模块查看商品信息")
    public Result<List<CouponProductVo>> listCouponProductInfo(@RequestParam("productIds") List<Long> productIds) {
        List<CouponProductVo> res = productService.listCouponProductInfo(productIds);
        return Result.success(res);
    }


    @GetMapping("/list")
    @Operation(summary = "获取商品列表")
    public Result<PageResult<ProductListInfoVo>> listProduct(
            @RequestParam(value = "productName", required = false) String productName,
            @RequestParam(value = "brandName", required = false) String brandName,
            @RequestParam(value = "productType", required = false) Integer productType,
            @RequestParam(value = "productId", required = false) Long productId,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "productStatus", required = false) Integer productStatus,
            @RequestParam(value = "currentPage") Long currentPage,
            @RequestParam(value = "pageSize") Long pageSize
    ) {
        PageResult<ProductListInfoVo> res = productService
                .listProduct(productName, brandName, productType, productId, categoryId, productStatus, currentPage, pageSize);
        return Result.success(res);
    }


}
